{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1935ae9",
   "metadata": {},
   "source": [
    "如果给定一个list或tuple，我们可以通过for循环来遍历这个list或tuple，这种遍历我们称为迭代。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41ffc486",
   "metadata": {},
   "source": [
    "python中的for循环不仅可以用在list或tuple上，还可以作用在其他可迭代对象上。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "abb96663",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "d = {'a':1, 'b':2,'c':3}\n",
    "for key in d:\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4873f12f",
   "metadata": {},
   "source": [
    "默认情况下，dict迭代的是key，如果要迭代value，可以用for value in d.values()。如果要同时迭代key和value，\n",
    "可以用for k, v in d.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0391bd8a",
   "metadata": {},
   "source": [
    "字符串也可以作为可迭代对象："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "38ec6b4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "B\n",
      "C\n"
     ]
    }
   ],
   "source": [
    "for ch in 'ABC':\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16e9f5d5",
   "metadata": {},
   "source": [
    "如何判断一个对象时可迭代对象？通过collections.abc模块的Iterable类型判断："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "554803a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections.abc import Iterable\n",
    "isinstance('abc',Iterable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd87a2b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(5,int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "63b84207",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(123,Iterable)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b1c50c0",
   "metadata": {},
   "source": [
    "要对list实现带有下标的循环，可以通过Python内置的enumerate函数可以把一个list变成索引-元素对："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d8221a48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 A\n",
      "1 B\n",
      "2 C\n"
     ]
    }
   ],
   "source": [
    "for i ,value in enumerate(['A', 'B', 'C']):\n",
    "    print(i, value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9c6270ee",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 1)\n",
      "(2, 4)\n",
      "(3, 9)\n"
     ]
    }
   ],
   "source": [
    "for r in [(1,1),(2,4),(3,9)]:\n",
    "    print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e8f0be89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1\n",
      "2 4\n",
      "3 9\n"
     ]
    }
   ],
   "source": [
    "for x, y in [(1,1),(2,4),(3,9)]:\n",
    "    print(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e942a46",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2588914",
   "metadata": {},
   "source": [
    "任何可以迭代对象都可以作用域for循环，包括我们自定义的数据类型，只要符合迭代条件即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56d1eced",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
